import { mapState } from 'vuex'
export default {
  /**
   * 自定义属性值
   */
  data() {
    return {
      list: [],
      searchDate: { start: '', end: '', placeholder: '请选择时间' }, // 搜索日期
    }
  },
  computed: {
    ...mapState('casic', {
      activeTheme: (state) => state.theme.activeName,
    }),
    /**
     * 搜索的开始时间
     */
    startTime() {
      if (!this.searchDate.start) return null
      return new Date(this.searchDate.start).getTime()
    },

    /**
     * 搜索的结束时间
     */
    endTime() {
      if (!this.searchDate.end) return null
      return new Date(this.searchDate.end).getTime() + 86400000 // 多加一天毫秒数
    },
  },
  /**
   * 挂载完成
   */
  mounted() {
    this.getList()
  },
  /**
   * 自定义方法
   */
  methods: {
    formatterTime(prop, row) {
      if (row[prop]) {
        return this.CORE.timeStr(row[prop])
      } else {
        return '--'
      }
    },
    /**
     * 关键词查询
     *
     * @param {*} page
     */
    toSearch() {
      this.model.pagination.page = 1
      this.getList()
    },
    toDetail(id) {
      const routePath = this.$route.path
      this.$router.push({ path: `${routePath}/${id}` })
    },
    /**
     * @description 改变配置项每页页数
     * @param {Number} pageSize -每页页数
     */
    handleSizeChange(pageSize) {
      this.model.pagination.pageSize = pageSize
      this.getList()
    },
    /**
     * 改变页码
     *
     * @param {*} page
     */
    changePage(page) {
      this.model.pagination.page = page
      this.getList()
    },
    /**
     * 列表数据
     *
     * @param {*} data
     */
    async getList(
      data = {
        // 搜索
        ...this.model.searchData,
        startTime: this.startTime,
        endTime: this.endTime,
        // 页码
        page: this.model.pagination.page,
        // 每页记录数
        pageSize: this.model.pagination.pageSize,
      },
    ) {
      const [num, arr] = await this.API.getList(data)
      this.list = arr
      this.model.pagination.total = num
    },
    /**
     * @description 设备导入
     * @param {String} content -二进制流
     */
    async importFromExcel(content) {
      if (!content) return
      try {
        const msgSuccess = await this.API.import_file({ content })
        this.$message.success(msgSuccess)
        this.getList()
      } catch (error) {
        error && this.$message.error(error.message)
      }
    },
    /**
     * 打开新增/修改弹窗
     *
     * @param {*} row
     * @param {*} openType
     * @param {*} refName
     */
    openModel(row, openType, refName = 'formModal') {
      this.$nextTick(() => {
        this.$refs[refName].init(row, openType)
      })
    },
    /**
     * 打开删除弹窗
     *
     * @param {*} row
     *
     */
    openDelete(row, title) {
      this.$nextTick(() => {
        this.$confirm(`确定删除此条${title}?`, `删除${title}`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error',
        })
          .then(() => {
            this.sureDelete(row)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
      })
    },
    /**
     * @description:确认删除
     * @param {*}
     * @return {*}
     */
    async sureDelete(row) {
      try {
        const msgSuccess = await this.API.del(row)
        this.$message.success(msgSuccess)
        this.getList()
      } catch (error) {
        error && this.$message.error(error.message)
      }
    },
  },
}
